home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / st.c < prev    next >
C/C++ Source or Header  |  1992-08-19  |  20KB  |  1,004 lines

  1. /* OS- and machine-dependent stuff for Atari-ST
  2.  * Adapted from the PC version to compile under Lattice C
  3.  * by Walter Doerr, DG2KK (dg2kk@cup.portal.com)
  4.  *
  5.  * 20-2-88:    added code from the Atari MWC version by Rob Janssen PE1CHL 
  6.  * 13-1-88:    added code needed for the 871225.1 version
  7.  * 24-12-87:    first version, adapted from PC.C from the 870412 release
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include "global.h"
  12. #include "config.h"
  13. #include "mbuf.h"
  14. #include "internet.h"
  15. #include "iface.h"
  16. #include "st.h"
  17. #include "cmdparse.h"
  18. #include "asy.h"
  19.  
  20. /* #include "stdlib.h"    chkml() , malloc , free */
  21.  
  22. #ifdef LATTICE
  23. #include "dos.h"    /* dfind */
  24. #endif
  25. #include "osbind.h"
  26.  
  27. #ifdef    MWC
  28. #include <stat.h>
  29. #include <path.h>
  30. #include <time.h>
  31. #include <signal.h>
  32. FILE *stdprt;
  33. extern    char **environ;
  34. #endif
  35. #if !defined( GNUC) && !defined(MWC)
  36. #define Crawcin() gemdos(7) 
  37. #endif
  38.  
  39.  
  40.  
  41. int Sixteen;
  42.  
  43. #define TRUE -1
  44. #define FALSE 0
  45.  
  46. struct asy asy[ASY_MAX];
  47.  
  48. /* Interface list header */
  49. struct interface *ifaces;
  50.  
  51. static struct TempList {
  52.     struct TempList *next;
  53.     char *name;
  54. } *Head;
  55.  
  56. unsigned nasy;            /* needed in v871225.1 */
  57.  
  58. char *ttbuf;
  59. char *rsbuf;            /* location of memory alloc'd for rs232 buf */
  60.  
  61. #ifdef SCREEN
  62. char *newscreen, *newscradr;    /* 32k memory allocated to 2nd video screen */
  63. long logscreen, physcreen;
  64. int toggle = 0;            /* toggle between normal and trace screen */
  65. int printtrace = 0;        /* send trace to printer (controlled by F2) */
  66. #endif
  67.  
  68. char *shell,*getenv();        /* Name of command shell for exec */
  69. int    rows=25;        /* Number of text rows on screen (may be 50) */
  70.  
  71. /* called during init b4 anything is printed on the screen.
  72.  * PC.C does a lot of memory allocation stuff here.
  73.  * We just set the cursor to "blink" and allocate memory for a second video
  74.  * screen.
  75.  */
  76. ioinit()
  77. {
  78.        
  79.     char *lmalloc();    /* Takes a long arg */
  80.     unsigned long ptr;
  81. #ifdef GNUC
  82. /* hope GCC has not been set to 16bit int mode ! */
  83. #define lmalloc(s) malloc(s)
  84. #endif
  85. #ifdef SCREEN
  86.     newscradr = lmalloc(32*1024L);    /* allocate 32k for 2nd video screen */
  87.     ptr = (unsigned long)newscradr;
  88.     newscreen = (char *)((ptr+255) & 0xffffff00L);    /* 256 byte boundary */
  89.  
  90.     physcreen = (long)Physbase();    /* remember displayed screen address */
  91.     logscreen = (long)Logbase();    /* remember output screen address */
  92.     Cconws("\033j");        /* Save cursor position */
  93.     (void) Setscreen(newscreen,-1L,-1);    /* switch to 2nd screen */
  94.     (void) Vsync();            /* wait for vsync */
  95.     Cconws("\033H\033J");        /* cursor home, clear screen */
  96.     Cconws("\033k");        /* restore cursor position */
  97.     (void) Setscreen(logscreen,-1L,-1);    /* restore old screen */
  98.     (void) Vsync();            /* wait for vsync */
  99. #endif
  100.     printf("\033v\033e\n");        /* Autowrap on, Cursor on */
  101.     (void) Cursconf(2,0);        /* make the cursor blink (3=steady) */
  102. #ifdef MWC
  103.     stdprt = fopen("prn:","w");
  104.     signal(SIGINT, SIG_IGN);    /* Ignore ^C in GEMDOS I/O  -- hyc */
  105. #endif
  106.  
  107.     shell = getenv("NROWS");
  108.     if (shell != NULL)
  109.         rows=atoi(shell);
  110.  
  111.     shell = getenv("SHELL");
  112.     if (shell == NULL)
  113.         shell="\\bin\\gulam.prg";
  114. }
  115.  
  116. /* Called just before exiting. 
  117.  * delete temp files.
  118.  * free memory allocated to the rs-232 and midi buffers.
  119.  */
  120. iostop()
  121. {
  122.     register struct TempList *tptr;
  123.  
  124.     /* free memory allocated to 2nd video screen */
  125.  
  126. #ifdef SCREEN
  127.     dispscreen(0);        /* switch back to original screen */
  128.     free(newscradr);
  129. #endif
  130.  
  131. #if  !defined(MWC) && !defined(GNUC)
  132.     /* delete all temp files that have accumulated. */
  133.     /* MWC does not create temp files, GNUC does, but a tmpdel()
  134.      * function will have to wait.  Just use a pooper-scooper for now.
  135.      */
  136.     (void) tmpdel();
  137. #endif
  138.  
  139.     /* free memory allocated to RS-232/MIDI I/O buffers */
  140.     while (ifaces != NULLIF) {
  141.         if (ifaces->stop != NULLFP)
  142.             (*ifaces->stop)(ifaces);
  143.         ifaces = ifaces->next;
  144.     }
  145.  
  146.     tptr = Head;            /* Delete all temp files */
  147.     while( tptr != NULL) {
  148.         if(tptr->name != NULL)
  149.             unlink(tptr->name);
  150.         tptr = tptr->next;
  151.     }
  152.     printf("\n");
  153. }
  154.  
  155. #ifndef    MWC
  156. /* checks the time then ticks and updates ISS */
  157. static int32 clkval = 0;
  158. void
  159. check_time()
  160. {
  161.     int32 iss();            /* initial sequence number */
  162.     int32 clksec();
  163.     if(clkval != clksec()){
  164.          clkval = clksec();
  165.         icmpclk();
  166.         tick();
  167.         (void)iss();
  168.     }
  169. }
  170.  
  171. /* returns the number of seconds from system clock */
  172. static int32
  173. clksec()
  174. {
  175.     long tloc;
  176.     time(&tloc);
  177.     return (tloc);
  178. }
  179. #else
  180. static    clock_t    clkval=0;
  181. void
  182. check_time()
  183. {
  184.     int32 iss();
  185.  
  186.     /* System clock is 200Hz or 5ms/tick. Skip 11 ticks to match the
  187.      * definition in timer.h (MSPTICK = 55 there.) -- hyc
  188.      */
  189.     if((clock() - clkval) > 10) {
  190.         clkval = clock();
  191.         icmpclk();
  192.         tick();
  193.         (void)iss();
  194.     }
  195. }
  196. #endif
  197.  
  198. /* Initialize async port "dev" (adapted from PE1CHL) */
  199. int
  200. asy_init(dev,addr,vec,bufsize)
  201. int16 dev;
  202. char *addr, *vec;
  203. unsigned bufsize;
  204. {
  205.     register struct iorec *ip;
  206.     register struct asy *ap;
  207.     char *bufp;
  208.  
  209. #ifdef DEBUG
  210.     printf("asy_init: dev=%d bufsize=%d\n",dev,bufsize);
  211.     fflush(stdout);
  212. #endif
  213.     ap = &asy[dev];
  214.  
  215. /* ---- Moved here from slip.c - originally by DG2KK... -- hyc ----- */
  216.     /* addr    (COM Port address) is the Atari device name
  217.      *         (either "AUX:" or "MIDI")
  218.      * vec    (Interrupt vector) is used as a flag to indicate if 
  219.      *         bytes received on that interface should be sent out on
  220.      *         another interface (1 = AUX: 3 = MIDI).
  221.      */
  222.     ap->vec = atoi(vec);        /* dev to resend bytes to */
  223.     ap->addr = 0;            /* use as error flag */
  224.     if (strcmp(addr,"AUX:") == 0) {
  225.         ap->addr = 1;
  226.     } else if (strcmp(addr,"CON:") == 0) {    /* This would be stupid. */
  227.         ap->addr = 2;
  228.     } else if (strcmp(addr,"MIDI") == 0) {
  229.         ap->addr = 3;
  230.     }
  231. /* ----- end of paraphrase of slip.c... -- hyc ------- */
  232.     if (ap->addr == 0) {
  233.         printf("asy_init(%d): unknown interface\n",dev);
  234.         return -1;
  235.     }
  236.  
  237.     /* force user to allocate more memory than he already has.
  238.      * If no memory is allocated, asy_stop() may behave funny...
  239.      */
  240.     if (bufsize <= 256)    /* only allocate a bigger buffer */
  241.         return -1;
  242.  
  243.     if ((bufp = malloc(bufsize)) == NULLCHAR){
  244.         printf("asy_init(%d): no memory for rx buffer\n",dev);
  245.         return -1;
  246.     }
  247.  
  248.     /* Save original IOREC values */
  249.  
  250.     ip = Iorec((ap->addr)-1);    /* Iorec wants AUX: = 0, MIDI = 2 */
  251.  
  252.     Jdisint(12);            /* disable RS-232 interrupt */
  253.  
  254.     ap->in = ip;
  255.     memcpy(&ap->oldin,ip, sizeof(struct iorec));  /* prev version was 
  256.                                * not right ! */
  257.     if (ap->addr == RS232) {    /* increase RS-232 transmit buffer? */
  258.         ip++;
  259.         ap->out = ip;
  260.         memcpy(&ap->oldout,ip,sizeof(struct iorec));
  261.     }
  262.  
  263.     /* Set up receiver FIFO */
  264.     ap->in->ibuf = bufp;
  265.     ap->in->ibufsiz = bufsize;
  266.     ap->in->ibufhd = ap->in->ibuftl = 0;
  267.     ap->in->ibuflow = 0;
  268.     ap->in->ibufhi = bufsize;
  269.  
  270.     if (ap->addr == RS232) {
  271.         /* clear transmitter FIFO */
  272.         ap->out->ibufhd = ap->out->ibuftl = 0;
  273.         ap->out->ibuflow = 0;
  274.         ap->out->ibufhi = ap->out->ibufsiz;
  275.     }
  276.  
  277.     Jenabint(12);            /* enable RS-232 interrupts */
  278.  
  279.     if (ap->addr == RS232)
  280.         Rsconf(-1,0,-1,0,0,0);    /* 8 bits, no parity */
  281.  
  282. #ifdef DEBUG
  283.     printf("asy_init: Iorecs in: 0x%lx out: 0x%lx\n",ap->in,ap->out);
  284.     printf("    inbuf: 0x%lx outbuf: 0x%lx\n",ap->in->ibuf,
  285.         ap->out->ibuf);
  286. #endif
  287. }
  288.  
  289.  
  290. /* asy_stop restores old iorec and frees memory allocated to the RS-232/MIDI
  291.  * buffers. (from PE1CHL)
  292.  */
  293. int
  294. asy_stop(iface)
  295. struct interface *iface;
  296. {
  297.     register struct asy *ap;
  298. /*    char i_state; */
  299.  
  300. #ifdef DEBUG
  301.     printf("asy_stop: iface=0x%lx dev=%d\n",iface,iface->dev);
  302.     fflush(stdout);
  303. #endif
  304.     ap = &asy[iface->dev];
  305.  
  306.     (void) Jdisint(12);        /* disable RS-232 interrupts */
  307.  
  308.     free(ap->in->ibuf);        /* free the buffer */
  309.  
  310.     /* Restore old iorecs */
  311.     memcpy(ap->in,&ap->oldin,sizeof(struct iorec));
  312.     if (ap->addr == RS232)
  313.         memcpy(ap->out,&ap->oldout,sizeof(struct iorec));
  314.  
  315.     (void) Jenabint(12);        /* enable RS-232 interrupts */ 
  316. }
  317.  
  318.  
  319. /* Set async line speed */
  320. int
  321. asy_speed(dev,speed)
  322. int dev;
  323. int speed;
  324. {
  325.     int baud; /* int result; */
  326.     register int sp;
  327.     long sav_ssp;
  328.  
  329.     if (speed <= 0 || dev >= nasy)
  330.         return -1;
  331.  
  332.     asy[dev].speed = speed;        /* shouldn't this be done in slip.c? */
  333.  
  334.     switch (asy[dev].addr) {
  335.  
  336.     case RS232:
  337.         switch (speed) {
  338.         case 300:
  339.             baud = 9;    /* how slow can you get? :-) */
  340.             break;
  341.         case 1200:
  342.             baud = 7;
  343.             break;
  344.         case 2400:
  345.             baud = 4;
  346.             break;
  347.         case 4800:
  348.             baud = 2;
  349.             break;
  350.         case 9600:
  351.             baud = 1;
  352.             break;
  353.         case 19200:
  354.             baud = 0;
  355.             break;